layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;



    var companyId =  Feng.getUrlParam("companyId");
    /**
     * 拟实施措施计划管理
     */
    var TProposedMeasuresPlan = {
        tableId: "tProposedMeasuresPlanTable"
    };

    /**
     * 初始化表格的列
     */
    TProposedMeasuresPlan.initColumn = function () {
        return [[
            {field: 'redType', sort: true, title: '减排类型',templet: function (d) {
                    if(d.redType == 1){
                        return '源头替代';
                    } else if(d.redType == 2){
                        return '过程控制';
                    } else {
                        return '末端治理';
                    }
                }},
            {field: 'measureType', sort: true, title: '措施类型',templet: function (d) {
                    if(d.measureType == 1){
                        return '强制性措施';
                    } else if(d.measureType == 2){
                        return '推荐性措施';
                    }
                }},
            {field: 'measureInfo', sort: true, title: '措施内容'},
            {field: 'planTime', sort: true, title: '计划完成时间'},
            {field: 'estimate', sort: true, title: '减排估算'},
            {field: 'units', sort: true, title: '单位'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TProposedMeasuresPlan.search = function () {
        var queryData = {};
        queryData['companyName'] = $("#companyName").val();
        table.reload(TProposedMeasuresPlan.tableId, {
            where: queryData, page: {curr: 1}
        });
    };






    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TProposedMeasuresPlan.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tProposedMeasuresPlan/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TProposedMeasuresPlan.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TProposedMeasuresPlan.tableId,
        url: Feng.ctxPath + '/tMeasures/measureInfo?companyId=' +companyId,
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TProposedMeasuresPlan.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TProposedMeasuresPlan.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TProposedMeasuresPlan.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TProposedMeasuresPlan.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TProposedMeasuresPlan.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TProposedMeasuresPlan.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TProposedMeasuresPlan.onDeleteItem(data);
        }else if(layEvent === "measureInfo"){
            TProposedMeasuresPlan.openMeasureInfo(data);
        }else if(layEvent === "ledgerInfo"){
            TProposedMeasuresPlan.ledgerInfo(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

        TProposedMeasuresPlan.changeDelFlag(id, checked);
    });



    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/tMeasures/measureHeadInfo?companyId=" +companyId);
    var result = ajax.start();
    form.val('tProposedMeasuresPlanForm', result.data);
    if (result.data.totalMeasure!=null&&result.data.totalMeasure!=""){
            $('#totalMeasure').val(result.data.totalMeasure)
    }



});
